package so.ifl.servcie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.dao.RoleInfoDao;
import so.ifl.servcie.RoleService;
import so.ifl.vo.RoleVo;

/**
 *create by aaron.lu
 *create at 2012-1-15 上午12:27:51
 *<mailto: atyuan.lu@gmail.com>
 **/
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleInfoDao roleInfoDao;
	
	/**
	 * 查询角色
	 * create by aaron.lu
	 * create at 2012-1-15 上午12:50:20
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params 参数集
	 * @param offset 分页查询起始记录
	 * @param limit 分页量
	 * @return
	 *
	 */
	public List<RoleVo> queryRoleWithParamAndPagination(Map<String, Object> params, int offset, int limit){
		return roleInfoDao.queryRoleWithParamAndPagination(params, new RowBounds(offset, limit));
	}
	
	/**
	 * 统计角色
	 * create by aaron.lu
	 * create at 2012-1-15 上午12:57:35
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public int countRoleWithParam(Map<String, Object> params){
		return roleInfoDao.countRoleWithParam(params);
	}
	
	/**
	 * 新增角色
	 * create by aaron.lu
	 * create at 2012-1-15 上午12:58:11
	 * <mailto: atyuan.lu@gmail.com>
	 * @param roleVo
	 *
	 */
	public void insertRole(RoleVo roleVo){
		roleInfoDao.insertRole(roleVo);
	}
	
	/**
	 * 删除角色
	 * create by aaron.lu
	 * create at 2012-1-15 上午1:00:03
	 * <mailto: atyuan.lu@gmail.com>
	 * @param roleId
	 *
	 */
	public void deleteRole(Integer roleId){
		roleInfoDao.deleteRole(roleId);
	}
	
	/**
	 * 查询角色与菜单关系
	 * create by aaron.lu
	 * create at 2012-1-17 下午8:31:48
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public List<Map<String, Object>> queryRole2Menu(Map<String, Object> params){
		return roleInfoDao.queryRole2Menu(params);
	}
	
	/**
	 * 新增角色与菜单的关系
	 * create by aaron.lu
	 * create at 2012-1-17 下午8:56:00
	 * <mailto: atyuan.lu@gmail.com>
	 * @param menuId
	 * @param roleId
	 *
	 */
	public void insertRelationRole2Menu(String menuId, String roleId){		
		Map<String, Object> params  = new HashMap<String, Object>();
		params.put("menu_id_", menuId);
		params.put("role_id_", roleId);
		
		List<Map<String, Object>> list = roleInfoDao.queryRole2Menu(params);
		if (null != list && list.size() == 0) {
			roleInfoDao.insertRelationRole2Menu(params);
		}		
	}
	
	/**
	 * 删除菜单与角色关系
	 * create by aaron.lu
	 * create at 2012-1-17 下午8:34:58
	 * <mailto: atyuan.lu@gmail.com>
	 * @param relationId
	 *
	 */
	public void deleteRelationRole2Menu(Integer relationId){
		roleInfoDao.deleteRelationRole2Menu(relationId);
	}
	
	/**
	 * 通过角色ID和菜单ID删除关系
	 * create by aaron.lu
	 * create at 2012-1-17 下午8:55:23
	 * <mailto: atyuan.lu@gmail.com>
	 * @param menuId
	 * @param roleId
	 *
	 */
	public void deleteByMRID(String menuId, String roleId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("menu_id_", menuId);
		params.put("role_id_", roleId);
		
		List<Map<String, Object>> list = roleInfoDao.queryRole2Menu(params);
		
		if (null != list && list.size() > 0) {
			Map<String, Object> oneR = list.get(0);
			Integer relationId = (Integer)oneR.get("menu_mm_role_id_");
			
			deleteRelationRole2Menu(relationId);
		}
	}
}
